package com.itzg.filestats;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

public class SaveScenarioDlg extends JDialog {
	private static final long serialVersionUID = 1L;
	private JTextField scenarioName;
	private JTextField fieldShowingFile;
	private File scenarioFileToSave;
	private boolean saved;

	public SaveScenarioDlg(Analyzer analyzer) {
		setSize(new Dimension(485, 160));
		setModal(true);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				getFocusTraversalPolicy().getDefaultComponent(scenarioName);
			}
		});
		setTitle("Save scenario from "+analyzer.getStartingDirectory());
		initComponents();
	}

	protected void initComponents() {
		BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();
		borderLayout.setVgap(10);
		borderLayout.setHgap(10);
		
		JPanel inputPnl = new JPanel();
		getContentPane().add(inputPnl, BorderLayout.CENTER);
		
		JLabel lblScenarioName = new JLabel("Scenario name:");
		lblScenarioName.setFont(UIManager.getFont("Label.font"));
		
		JLabel lblScenarioFile = new JLabel("Scenario file:");
		
		scenarioName = new JTextField();
		scenarioName.setColumns(10);
		
		JButton button = new JButton("...");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				browseToSaveScenarioFile();
			}
		});
		
		fieldShowingFile = new JTextField();
		fieldShowingFile.setEditable(false);
		fieldShowingFile.setColumns(10);
		GroupLayout gl_inputPnl = new GroupLayout(inputPnl);
		gl_inputPnl.setHorizontalGroup(
			gl_inputPnl.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_inputPnl.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_inputPnl.createParallelGroup(Alignment.LEADING)
						.addComponent(lblScenarioName)
						.addComponent(lblScenarioFile))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_inputPnl.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_inputPnl.createSequentialGroup()
							.addComponent(fieldShowingFile, GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(button))
						.addComponent(scenarioName, GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_inputPnl.setVerticalGroup(
			gl_inputPnl.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_inputPnl.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_inputPnl.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblScenarioName)
						.addComponent(scenarioName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_inputPnl.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblScenarioFile)
						.addComponent(button)
						.addComponent(fieldShowingFile, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(169, Short.MAX_VALUE))
		);
		inputPnl.setLayout(gl_inputPnl);
		
		JPanel actionPnl = new JPanel();
		FlowLayout flowLayout = (FlowLayout) actionPnl.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(actionPnl, BorderLayout.SOUTH);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleSave();
			}
		});
		actionPnl.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeDialog();
			}
		});
		actionPnl.add(btnCancel);
		
	}

	protected void handleSave() {
		saved = true;
		closeDialog();
	}

	protected void closeDialog() {
		setVisible(false);
	}

	protected void browseToSaveScenarioFile() {
		JFileChooser chooser = new JFileChooser();
		chooser.setDialogType(JFileChooser.SAVE_DIALOG);
		chooser.addChoosableFileFilter(new FileFilter() {
			
			@Override
			public String getDescription() {
				return "XML files (*.xml)";
			}
			
			@Override
			public boolean accept(File f) {
				return f.isDirectory() ||
						f.getName().toLowerCase().endsWith(".xml");
			}
		});
		
		int result = chooser.showSaveDialog(this);
		if (result == JFileChooser.APPROVE_OPTION) {
			setScenarioFileToSave(chooser.getSelectedFile());
		}
	}

	public File getScenarioFileToSave() {
		return scenarioFileToSave;
	}

	public void setScenarioFileToSave(File scenarioFileToSave) {
		this.scenarioFileToSave = scenarioFileToSave;
		if (scenarioFileToSave != null) {
			fieldShowingFile.setText(scenarioFileToSave.getPath());
		}
		else {
			fieldShowingFile.setText("");
		}
	}
	
	public String getScenarioName() {
		return scenarioName.getText();
	}

	public boolean isSaved() {
		return saved;
	}
}
